package com.bianmaba.example.redis.controller;

import com.bianmaba.commons.bean.result.OperationResult;
import com.bianmaba.example.redis.beans.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("object")
public class ObjectController {

    @Autowired
    private RedisTemplate<String, Object> objectRedisTemplate;

    @RequestMapping("put")
    public OperationResult put() {
        String key = "test_key";
        objectRedisTemplate.opsForValue().set(key, new User("1", "name1"), Duration.ofSeconds(10));
        Object value = objectRedisTemplate.opsForValue().get(key);
        return OperationResult.of(true, "操作成功", value);
    }

    /**
     * 左则加入类似先进后出，即元素取出后与加入顺序相反
     *
     * @return
     */
    @RequestMapping("list_push_left")
    public OperationResult listPushLeft() {
        String key = "list_key";
        objectRedisTemplate.opsForList().leftPushAll("list_key", new User("1", "name1"), new User("2", "name2"));
        //设置过期时间
        objectRedisTemplate.expire(key, 10, TimeUnit.SECONDS);

        objectRedisTemplate.opsForList().leftPush("list_key", new User("add_3", "add_name3"));
        List<Object> value = objectRedisTemplate.opsForList().range(key, 0, -1);
        return OperationResult.of(true, "操作成功", value);
    }

    /**
     * 右则加入类似先进先出，即元素取出后与加入顺序相同
     *
     * @return
     */
    @RequestMapping("list_push_right")
    public OperationResult listPushRight() {
        String key = "list_key";
        objectRedisTemplate.opsForList().rightPushAll("list_key", new User("1", "name1"), new User("2", "name2"));
        //设置过期时间
        objectRedisTemplate.expire(key, 10, TimeUnit.SECONDS);

        objectRedisTemplate.opsForList().rightPush("list_key", new User("add_3", "add_name3"));
        List<Object> value = objectRedisTemplate.opsForList().range(key, 0, -1);
        return OperationResult.of(true, "操作成功", value);
    }

    @RequestMapping("put_map")
    public OperationResult putMap() {
        String key = "map_key";

        objectRedisTemplate.opsForHash().putAll(key, new HashMap() {{
            put("user1", new User("1", "name1"));
            put("user2", new User("2", "name2"));
        }});
        objectRedisTemplate.opsForHash().put(key, "add_user3", new User("add_3", "add_name3"));
        //设置过期时间
        objectRedisTemplate.expire(key, 10, TimeUnit.SECONDS);

        User user = (User) objectRedisTemplate.opsForHash().get(key, "add_user3");
        System.out.println("从map中取单值email:" + user);

        Map<Object, Object> value = objectRedisTemplate.opsForHash().entries(key);
        return OperationResult.of(true, "操作成功", value);
    }
}
